<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    async function* ints() {
      for (let i = 0; i < 5; ++i) {
        yield await new Promise(resolve => setTimeout(resolve, 1000, i));
      }
    }

    () => {
      const readableStream = new ReadableStream({
        async start(controller) {
          for await (const chunk of ints()) {
            controller.enqueue(chunk);
          }
          controller.close();
        },
      });

      console.log(readableStream.locked);
      const readableStreamDefaultReader = readableStream.getReader();
      console.log(readableStream.locked);

      (async () => {
        while (true) {
          const { done, value } = await readableStreamDefaultReader.read();
          if (done) {
            break;
          } else {
            console.log(value);
          }
        }
      })();
    };

    () => {
      const writableStream = new WritableStream({
        write(value) {
          console.log(value);
        },
      });

      console.log(writableStream.locked);
      const writableStreamDefaultWriter = writableStream.getWriter();
      console.log(writableStream.locked);

      (async () => {
        for await (const chunk of ints()) {
          await writableStreamDefaultWriter.ready;
          writableStreamDefaultWriter.write(chunk);
        }
        writableStreamDefaultWriter.close();
      })();
    };

    () => {
      const { writable, readable } = new TransformStream({
        transform(chunk, contoller) {
          contoller.enqueue(chunk * 2);
        },
      });

      const readableStreamDefaultReader = readable.getReader();
      const writableStreamDefaultWriter = writable.getWriter();

      // 消费者
      (async () => {
        while (true) {
          const { done, value } = await readableStreamDefaultReader.read();

          if (done) {
            break;
          } else {
            console.log(value);
          }
        }
      })();

      // 生产者
      (async () => {
        for await (const chunk of ints()) {
          await writableStreamDefaultWriter.ready;
          writableStreamDefaultWriter.write(chunk);
        }
        writableStreamDefaultWriter.close();
      })();
    };

    () => {
      const integerStream = new ReadableStream({
        async start(controller) {
          for await (const chunk of ints()) {
            controller.enqueue(chunk);
          }
          controller.close();
        },
      });

      const doublingStream = new TransformStream({
        transform(chunk, contoller) {
          contoller.enqueue(chunk * 2);
        },
      });

      const pipedStream = integerStream.pipeThrough(doublingStream);
      const pipedStreamDefaultReader = pipedStream.getReader();

      (async () => {
        while (true) {
          const { done, value } = await pipedStreamDefaultReader.read();

          if (done) {
            break;
          } else {
            console.log(value);
          }
        }
      })();
    };
  </script>
</body>

</html>